using Engine.FileFormats3D.WR2TrafficFiles;
using Engine.TrafficHelpers;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Scenario_Editor.CustomControls
{
    public partial class TrafficControl : UserControl
    {
        #region Private Fields

        private CrossroadTrafficData crossroadTrafficData;
        private bool duringControlUpdate = false;

        #endregion Private Fields

        #region Public Constructors

        public TrafficControl()
        {
            InitializeComponent();
            this.crossroadTrafficData.Lane = new Lane((short)this.numericUpDown_NumberOfLanesRight.Value,
                (float)this.numericUpDown_OffsetLane1.Value,
                (float)this.numericUpDown_OffsetLane2.Value);

            //this.crossroadTrafficData.Speed = (float)this.numericUpDown_Speed.Value;
            //this.crossroadTrafficData.TangentLengthEnd =
            OnValueChange(null, null);
        }

        #endregion Public Constructors

        #region Public Delegates

        public delegate void ValueChanged(object sender, EventArgs e);

        #endregion Public Delegates

        #region Public Events

        public event ValueChanged valueChanged;

        #endregion Public Events

        #region Public Properties

        [DefaultValue("Name")]
        public string TextForGroupbox
        {
            get { return this.groupBox_RightLane.Text; }
            set { this.groupBox_RightLane.Text = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public CrossroadTrafficData GetCrossroadTrafficData()
        {
            return crossroadTrafficData;
        }

        public void SetCrossroadTrafficData(CrossroadTrafficData crossroadTrafficData)
        {
            this.crossroadTrafficData = crossroadTrafficData;
            UpdateControls();
        }

        #endregion Public Methods

        #region Private Methods

        private void OnValueChange(object sender, EventArgs e)
        {
            if (!this.duringControlUpdate)
            {
                Lane lane = this.crossroadTrafficData.Lane;
                lane.numberofLanes = (short)this.numericUpDown_NumberOfLanesRight.Value;
                lane.Offset1 = (float)this.numericUpDown_OffsetLane1.Value;
                lane.Offset2 = (float)this.numericUpDown_OffsetLane2.Value;
                this.crossroadTrafficData.Lane = lane;
                this.crossroadTrafficData.Enable = this.checkBox_EnableRightLane.Checked;
                this.crossroadTrafficData.Speed = (float)this.numericUpDown_Speed.Value;
                this.crossroadTrafficData.TangentLengthStart = (float)this.numericUpDown_TangentLengthStart.Value;
                this.crossroadTrafficData.TangentLengthEnd = (float)this.numericUpDown_TangentLengthEnd.Value;

                if (this.valueChanged != null)
                    this.valueChanged(this, null);
            }

            this.numericUpDown_OffsetLane2.Enabled = (this.numericUpDown_NumberOfLanesRight.Value == 2) ? true : false;
        }

        private void SelectTextOnEnter(object sender, EventArgs e)
        {
            if (sender.GetType() == typeof(NumericUpDown))
            {
                NumericUpDown numericUpDown = sender as NumericUpDown;
                numericUpDown.Select(0, numericUpDown.ToString().Length);
            }
        }

        private void UpdateControls()
        {
            this.duringControlUpdate = true;
            this.checkBox_EnableRightLane.Checked = this.crossroadTrafficData.Enable;
            this.numericUpDown_NumberOfLanesRight.Value = (decimal)this.crossroadTrafficData.Lane.numberofLanes;
            this.numericUpDown_OffsetLane1.Value = (decimal)this.crossroadTrafficData.Lane.Offset1;
            this.numericUpDown_OffsetLane2.Value = (decimal)this.crossroadTrafficData.Lane.Offset2;
            this.numericUpDown_Speed.Value = (decimal)this.crossroadTrafficData.Speed;
            this.numericUpDown_TangentLengthStart.Value = (decimal)this.crossroadTrafficData.TangentLengthStart;
            this.numericUpDown_TangentLengthEnd.Value = (decimal)this.crossroadTrafficData.TangentLengthEnd;

            this.duringControlUpdate = false;
        }

        #endregion Private Methods
    }
}